#! /usr/bin/perl

use strict;

#-------------------------------------------------
# Change THVBO read_insurance0_t record to
#   Payorintel read_insurance0_t record.
#
# Usage:
#    $script  $csv_file
#    cat $csv_file | $script
#
# Exit status
# 	0  -- Ok
# 	1  -- data file unreadable
# 	2  -- input data format not good
#-------------------------------------------------


#-------------------------------------------------
# Fields of table 'read_insurance0_t' in THVBO
#-------------------------------------------------
our @thvbo_read_ins1 = qw( 
  acctnum
  client_field1
  client_npi
  npi
  tax_id
  legacy
  pin
  entity_name
  entity_pay_to_address
  contact_information
  contact_phone
  cust_run_date
  relationship_flag
  service_date_begin
  balance
  g_lname
  g_mname
  g_fname
  g_addr1
  g_addr2
  g_city
  g_state
  g_zip5
  g_zip4
  g_ssn
  g_dob
  g_employ_name
  lname
  mname
  fname
  addr1
  addr2
  city
  state
  zip5
  zip4
  ssn
  gender
  dob
  employ_name
  provider_state
  client_field2
  client_field3
  );


#---------------------------------------------------
# Fields of table 'read_insurance0_t' in PayorIntel 
#---------------------------------------------------
our @payor_read_ins1 = qw(
  cust_id
  treatment_code
  output_id
  hid
  acctnum
  cpi
  client_npi1
  npi1
  tax_id1
  legacy1
  pin1
  entity_name1
  entity_pay_to_street1
  entity_pay_to_city1
  entity_pay_to_state1
  entity_pay_to_zip1
  entity_information1
  entity_phone1
  client_npi2
  npi2
  tax_id2
  legacy2
  pin2
  entity_name2
  entity_pay_to_street2
  entity_pay_to_city2
  entity_pay_to_state2
  entity_pay_to_zip2
  entity_information2
  entity_phone2
  lname
  mname
  fname
  addr1
  addr2
  city
  state
  zip5
  zip4
  ssn
  gender
  dob
  g_lname
  g_mname
  g_fname
  g_addr1
  g_addr2
  g_city
  g_state
  g_zip5
  g_zip4
  g_ssn
  g_gender
  g_dob
  relationship_flag
  g_employ_name
  provider_state
  service_date_begin
  service_date_end
  pat_proc_type
  pat_type
  cpt
  drg
  physician_code
  charges
  balance
  acct_status
  client_field1
  client_field2
  client_field3
  client_date1
);



our $indata = $ARGV[0] || "-";
our %dest;
our %input;
our $INFD = undef;

if( $indata ne "-" ) {
	if ( ! -r $indata ) {
		print STDERR " file $indata not accessable !!!\n";
		exit 1;
	}
	else {
		open(THV, "<$indata");
		$INFD = *THV;
	}
}
else {
	$INFD = *STDIN;
}

while(my $line = <$INFD> ) {
	chomp $line;
	my @tmp = split(/\|/, $line);
	if ( $#tmp != $#thvbo_read_ins1 ) {
		print STDERR " record line $. with wrong format !!!\n";
		print STDERR "--> $line\n";
		exit 2; #file format not matching
	}

	%dest=();
	%input=();
	#-------------------
	# populate hash var
	#-------------------
	for( my $i=0; $i<=$#tmp; $i++ ) {
		$input{$thvbo_read_ins1[$i]} = $tmp[$i];
	}
#print "$input{entity_pay_to_address}\n"; next;
	$dest{cust_id}	= 15;
	$dest{treatment_code} = 1;
	$dest{output_id}= 1;
	$dest{hid}		= 1;
	$dest{acctnum}  = $input{acctnum};
	$dest{client_npi1}	= $input{client_npi};
	$dest{npi1}		= $input{npi};
	$dest{tax_id1}	= $input{tax_id};
	$dest{legacy1}	= $input{legacy};
	$dest{pin1}		= $input{pin};

	$dest{lname}   = $input{lname};
	$dest{mname}   = $input{mname};
	$dest{fname}   = $input{fname};
	$dest{addr1}   = $input{addr1};
	$dest{addr2}   = $input{addr2};
	$dest{city}    = $input{city};
	$dest{state}   = $input{state};
	$dest{zip5}    = $input{zip5};
	$dest{zip4}    = $input{zip4};
	$dest{ssn}     = $input{ssn};
	$dest{gender}  = $input{gender};
	$dest{dob}     = $input{dob};
	$dest{g_lname} = $input{g_lname};
	$dest{g_mname} = $input{g_mname};
	$dest{g_fname} = $input{g_fname};
	$dest{g_addr1} = $input{g_addr1};
	$dest{g_addr2} = $input{g_addr2};
	$dest{g_city}  = $input{g_city};
	$dest{g_state} = $input{g_state};
	$dest{g_zip5}  = $input{g_zip5};
	$dest{g_zip4}  = $input{g_zip4};
	$dest{g_ssn}   = $input{g_ssn};
	$dest{g_dob}   = $input{g_dob};
	$dest{relationship_flag} = $input{relationship_flag};
	$dest{g_employ_name} = $input{g_employ_name};
	$dest{provider_state} = $input{provider_state};
	$dest{balance} = $input{balance};
	$dest{service_date_begin} = $input{service_date_begin};
	$dest{service_date_end}   = $input{service_date_begin};
	$dest{client_field1} = $input{client_field1};
	$dest{client_field2} = $input{client_field2};
	$dest{client_field3} = $input{client_field3};

	$dest{entity_name1}		= $input{entity_name};
	$dest{entity_information1}	= $input{contact_information};
	$dest{entity_phone1}		= $input{contact_phone};

	my $right=$input{entity_pay_to_address};
	$right  =~ s/^.*([A-Z]{2}\s+\d{5}-?(\d{4})?)\s*$/$1/;
	my $left=$input{entity_pay_to_address};
	$left =~ s/^(.*)${right}/$1/;
	
	$left =~ s/\s*$//;
	$left =~ s/\,$//;
	my $city = $left;
	$city	=~ s/^.*[,\s]+(\w+).*$/$1/;
	$dest{entity_pay_to_city1}	= $city;
	$left =~ s/^(.*)${city}\s*$/$1/;
	$left =~ s/\s*$//;
	$left =~ s/\,$//;
	$dest{entity_pay_to_street1}=$left;
	$dest{entity_pay_to_state1}	= $right;
	$dest{entity_pay_to_state1}	=~ s/^\s*([A-Z]{2}).*$/$1/;
	$dest{entity_pay_to_zip1}	= $right;
	$dest{entity_pay_to_zip1}	=~ s/^\s*[A-Z]{2}\s*(\d{5}-?(\d{4})?)/$1/;

	foreach my $fld ( @payor_read_ins1 ) {
		;#print "$fld : $dest{$fld}\n"; next;
		my $x = $dest{$fld};
		$x =~ s/^\s*//;
		$x =~ s/\s*$//;
		$x =~ s/\|//;
		print "$x";
		print "|" if $fld ne $payor_read_ins1[$#payor_read_ins1];
	}
	print "\n";
}
close(THV );
exit 0;


